#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    bool isMatch(string s, string p) {
        int n = p.size(), m = s.size();
        vector<vector<bool>> dp(n + 1, vector<bool>(m + 1, false));
        s = ' ' + s;
        p = ' ' + p;
        dp[0][0] = true;
        for (int i = 2; i <= n; i += 2) if (p[i] == '*') dp[i - 1][0] = dp[i][0] = true; else break;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                if (p[i] == '.' || p[i] == s[j]) dp[i][j] = dp[i - 1][j - 1];
                else if (p[i] == '*') dp[i][j] = dp[i - 2][j] || ((p[i - 1] == '.' || p[i - 1] == s[j]) ? dp[i][j - 1] : 0);
        return dp[n][m];
    }
};